<?php

class Factory_listApp extends MemberbaseApp {
    function __construct() {
        $this->Factory_listApp();
    }
    function Factory_listApp() {
        parent::__construct();
        //$this->check_powers(USER_ADMIN);
        $this->_factory_mod =& m('factory_info');
        $this->_type_mod =& m('factorytype');
    }
    function index() {
		$page = $this->_get_page();
		$user_info = $this->visitor->get();
		$where = 'activation=1 and role='.USER_FACTORY;
// 		if($user_info[role]==USER_QC){
// 			$qf_mod = & m('qcfactory');
// 			$qf_list = $qf_mod->find(array(
// 					'fields' => 'factory_id',
// 					'conditions' => 'user_id='.$user_info['user_id']
// 			));
// 			$arr_info = "";
// 			if($qf_list){
// 				$arr = array();
// 				foreach($qf_list as $key=>$value){
// 					$arr[$key] = $value['factory_id'];
// 				}
// 				$arr_info = implode(",",$arr);
// 				$where = empty($arr_info)?"factory_id=0":"factory_id in(".$arr_info.")";
// 			}
// 		}
		

        $factories = $this->_factory_mod->find(array(
        		'conditions' => $where,
                'join' => 'belongs_factory_member',
				'count' => true,
				'limit' => $page['limit']
        ));
		$page['item_count'] = $this->_factory_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
        $this->assign('factories', $factories);
        $this->display('factory.list.html');
        $this->_curlocal(Lang::get('factory_manage'));
    }
    
    //待激活工厂
    function activation(){
    	$page = $this->_get_page();
    	$user_info = $this->visitor->get();
    	$where = 'activation=0 and role='.USER_FACTORY;
    	$factories = $this->_factory_mod->find(array(
    			'conditions' => $where,
    			'join' => 'belongs_factory_member',
    			'count' => true,
    			'limit' => $page['limit']
    	));
    	$page['item_count'] = $this->_factory_mod->getCount();
    	$this->_format_page($page);
    	$this->assign('page_info', $page);
    	$this->assign('factories', $factories);
    	$this->display('factory.activation.html');
    	$this->_curlocal(Lang::get('factory_activation'));
    }
    
    function  show() {
        $factory_id = intval($_GET['fid']);
        if (!$factory_id) {
            $this->show_warning('factory_not_exist');
            return;
        }
        $factory = $this->_factory_mod->get(array(
        	'join' => 'belongs_factory_member',
        	'conditions'=>'factory_info.factory_id='.$factory_id
        ));
        if (!$factory) {
            $this->show_warning('factory_not_exist');
            return;
        }
        $file_mod=&m('upload');
        $factory['factory_images']=$file_mod->find(array(
        	'conditions'=>'file_type='.BELONGS_FACTORY_IMAGE.' and key_id='.$factory_id,
        	'order'=>'file_id desc'	
        ));
//         $types = $this->_type_mod->get(array(
//                 'conditions' => 'type_id = \''.$factory['type_id'].'\''
//         ));
//         $factory['type_name']=$types['type_name'];
        $this->assign('factory', $factory);
        $this->assign('user',$this->visitor->get());
        $this->display('factory.info.html');
    }
    function _get_types() {
        $types = $this->_type_mod->find(array(
                'conditions' => 'if_show = 1'
        ));
        $this->assign('types', $types);
    }
}

?>
